package com.example.zhish.gobang.util;

import android.graphics.Point;

import com.example.zhish.gobang.bean.Constant;
import com.example.zhish.gobang.bean.GobangCheckboard;

/**
 * Created by qianjun on 2016/5/1.
 */
public class GobangUtil {

    //默认是五子棋，可选六子棋
    public static int MAX_PIECES_WIN = Constant.FIVE_PIECES_WIN;
    public int table_x;
    public int table_y;
    public int[][] table;
    public static GobangUtil gobangUtil;

    public GobangUtil(int table_x, int table_y) {
        table = GobangCheckboard.getGobangCheckboard().table;
        this.table_x = table_x;
        this.table_y = table_y;
    }

    public static GobangUtil getGobangUtil(int table_x, int table_y) {
        if (gobangUtil == null) {
            gobangUtil = new GobangUtil(table_x, table_y);
        }
        return gobangUtil;
    }


    public int getTable_x() {
        return table_x;
    }


    public void setTable_x(int table_x) {
        this.table_x = table_x;
    }

    public int getTable_y() {
        return table_y;
    }

    public void setTable_y(int table_y) {
        this.table_y = table_y;
    }

    public static int getMaxPiecesWin() {
        return MAX_PIECES_WIN;
    }

    public static void setMaxPiecesWin(int maxPiecesWin) {
        MAX_PIECES_WIN = maxPiecesWin;
    }

    public int isWin(Point point) {

        int checkwinner = checkWinner(point);
        if (checkwinner==Constant.NO_VICTORY_OR_DEFEAT && GobangCheckboard.getGobangCheckboard().totalStep == (table_x * table_y)) {
            GobangCheckboard.getGobangCheckboard().isStop = true;
            return Constant.NO_WINNER;
        }

        return checkwinner;

    }

    private int checkWinner(Point point) {

        /**
         * 检查水平，垂直，左斜，右斜是否赢棋
         */
        boolean flag0 = checkHorizontal(point);
        boolean flag1 = checkVertical(point);
        boolean flag2 = checkLeft(point);
        boolean flag3 = checkRight(point);

        if (flag0 || flag1 || flag2 || flag3) {
            GobangCheckboard.getGobangCheckboard().isStop = true;
            return table[point.x][point.y] == Constant.BLACK_PIECE ? Constant.BLACK_WINNER : Constant.WHITE_WINNER;

        }


        return Constant.NO_VICTORY_OR_DEFEAT;
    }


    /**
     * 水平检查
     *
     * @param point
     * @return
     */
    private boolean checkHorizontal(Point point) {

        //棋子类型：黑子还是白子
        int pieceType = table[point.x][point.y];
        //记录连续出现的棋子数
        int count = 1;
        for (int i = (point.x - 1); i >= 0; i--) {
            if (table[i][point.y] != pieceType) {
                break;
            } else {
                count++;
            }
        }
        if (count >= MAX_PIECES_WIN) {
            return true;
        }

        for (int i = (point.x + 1); i < table_x; i++) {
            if (table[i][point.y] != pieceType) {
                break;
            } else {
                count++;
            }
        }
        if (count >= MAX_PIECES_WIN) {
            return true;
        }

        //没有获胜
        return false;
    }

    /**
     * 垂直检查
     *
     * @param point
     * @return
     */
    private boolean checkVertical(Point point) {
        //棋子类型：黑子还是白子
        int pieceType = table[point.x][point.y];
        //记录连续出现的棋子数
        int count = 1;
        for (int i = (point.y - 1); i >= 0; i--) {
            if (table[point.x][i] != pieceType) {
                break;
            } else {
                count++;
            }
        }
        if (count >= MAX_PIECES_WIN) {
            return true;
        }

        for (int i = (point.y + 1); i < table_y; i++) {
            if (table[point.x][i] != pieceType) {
                break;
            } else {
                count++;
            }
        }
        if (count >= MAX_PIECES_WIN) {
            return true;
        }

        //没有获胜
        return false;
    }

    /**
     * 左下右上检查
     *
     * @param point
     * @return
     */
    private boolean checkLeft(Point point) {
        //棋子类型：黑子还是白子
        int pieceType = table[point.x][point.y];
        //记录连续出现的棋子数
        int count = 1;
        for (int i = (point.x - 1), j = (point.y + 1); i >= 0 && j < table_y; i--, j++) {
            if (table[i][j] != pieceType) {
                break;
            } else {
                count++;
            }
        }
        if (count >= MAX_PIECES_WIN) {
            return true;
        }

        for (int i = (point.x + 1), j = (point.y - 1); i < table_x && j >= 0; i++, j--) {
            if (table[i][j] != pieceType) {
                break;
            } else {
                count++;
            }
        }
        if (count >= MAX_PIECES_WIN) {
            return true;
        }

        //没有获胜
        return false;
    }

    /**
     * 左上右下检测
     *
     * @param point
     * @return
     */
    private boolean checkRight(Point point) {
        //棋子类型：黑子还是白子
        int pieceType = table[point.x][point.y];
        //记录连续出现的棋子数
        int count = 1;
        for (int i = (point.x - 1), j = (point.y - 1); i >= 0 && j >= 0; i--, j--) {
            if (table[i][j] != pieceType) {
                break;
            } else {
                count++;
            }
        }
        if (count >= MAX_PIECES_WIN) {
            return true;
        }

        for (int i = (point.x + 1), j = (point.y + 1); i < table_x && j < table_y; i++, j++) {
            if (table[i][j] != pieceType) {
                break;
            } else {
                count++;
            }
        }
        if (count >= MAX_PIECES_WIN) {
            return true;
        }

        //没有获胜
        return false;
    }


}
